package com.shell.governor.user.rx;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.user.UserEvent;
import com.shell.governor.user.po.PubUserOpt;
import com.shell.governor.user.service.PubUserOptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UpdateUserOptSubscriber extends OnErrorNotImplementedSubscriber<UserEvent> {
	@Autowired
	PubUserOptService userOptService;

	public void onApplicationEvent(UserEvent event) {
		if ((!event.isNeedApprover()) && (event.getUserOpt().getBizCode().equals("0"))) {
			return;
		}
		PubUserOpt userOpt = event.getUserOpt();

		PubUserOpt lastOpt = this.userOptService.getLastOpt(userOpt.getUserId(), userOpt.getEntId());
		if (lastOpt != null) {
			userOpt.setSeqNo(Integer.valueOf(lastOpt.getSeqNo().intValue() + 1));
		} else {
			userOpt.setSeqNo(Integer.valueOf(0));
		}
		userOpt.setOptId(SystemUtil.getUUID());
		userOpt.setOprId(CommonDataUtil.getUserId());
		userOpt.setOprName(CommonDataUtil.getUserName());
		userOpt.setOprTime(DateUtil.getSystemDateTime());
		this.userOptService.save(userOpt);
	}
}
